package queues;

import helpers.DateHelper;
import helpers.IdUniqueHelper;
import helpers.QuestionCrawlerHelper;
import helpers.StringHelper;
import helpers.UrlHelper;

import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.AuthorModel;
import models.QuestionModel;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.google.gson.Gson;

import debugs.AnswerDebug;
import debugs.AuthorDebug;
import debugs.QuestionDebug;
import entities.Answer;
import entities.Author;
import entities.Question;

public class GetQuestion extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		String path = ((HttpServletRequest) request).getRequestURI();
		String url = URLDecoder.decode(path.replaceAll(request.getServletPath() + "/", ""),"UTF-8");
		
		response.getWriter().println(URLEncoder.encode(url));
		
		QuestionModel questionModel = QuestionModel.getInstance();
		Question question = questionModel.getQuestion(URLEncoder.encode(url));
		if (question == null) {
			question = QuestionCrawlerHelper.getQuestion(url, response);
			QuestionDebug.debugWeb(question, response);
		} else {
			response.getWriter().println("Question has been exists with url: " + url);
		}
	}
}
